package encaps.action;

import encaps.entities.Grant;
import encaps.logging.LogLocal;
import encaps.logging.LogLocalHome;
import encaps.utils.EntityUtil;
import encaps.utils.SystemException;
import encaps.utils.XMLHelper;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import org.jdom.Element;

public class DeleteLogAction extends Action {

    public static final String DELETE_LOG = "deleteLog";
    protected ActionContext actionContext;
    private Long id;

    public DeleteLogAction(ActionContext context, Grant requiredGrant) {
        this.actionContext = context;
        this.requiredGrant = requiredGrant;
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            LogLocalHome logHome = EntityUtil.getLogHome();
            id = Long.valueOf(actionContext.getTextParameter(ID));
            LogLocal log = logHome.findByPrimaryKey(id);
            log.remove();
            return XMLHelper.createTextMessage("Log entry removed");
        } catch (RemoveException ex) {
            throw new ActionExecutionException("Cannot remove log", ex);
        } catch (NamingException ex) {
            throw new ActionExecutionException("Cannot find LocalHome object", ex);
        } catch (FinderException ex) {
            throw new ActionExecutionException("Log " + id + " not found", ex);
        } catch (SystemException ex) {
            throw new ActionExecutionException("System malfunction", ex);
        }
    }
}
